package com.two.service.permission.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.two.common.entity.Permission;
import com.two.common.entity.RolePermission;
import com.two.common.entity.User;

import com.two.common.util.CommonResult;
import com.two.service.permission.mapper.PermissionMapper;
import com.two.service.permission.mapper.UserMapper;
import com.two.service.permission.service.IPermissionService;
import com.two.service.permission.service.IRolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 汪瑞明
 * @since 2022-08-29
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private IRolePermissionService rolePermissionService;

    // 通过用户id查询用户的所有权限
    @Override
    public List<Permission> selectPermissionByUserid(Integer userid) {
        List<Permission> permission = permissionMapper.selectPermissionByUserid(userid);
        return permission;
    }

    // 查询所有的用户信息
    @Override
    public User selectOne(String username) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("username",username);
        wrapper.eq("is_deleted",0);
        User user = userMapper.selectOne(wrapper);
        return user;
    }

    // 查询所有的权限
    @Override
    public CommonResult getLeftMenu(String token) {
        String m = redisTemplate.opsForValue().get(token);
        Map map = JSON.parseObject(m, Map.class);
        JSONObject user = (JSONObject) map.get("user");
        Integer userid = user.getInteger("userid");
        List<Permission> permissionList = permissionMapper.selectLeftPermission(userid);
        //设置层级关系
        List<Permission> firstMenus = new ArrayList<>();
        for (Permission first : permissionList) {
            //int
            if (first.getPid().equals(1)) {
                firstMenus.add(first);
            }
        }


        //为一级菜单设置二级菜单
        for (Permission first : firstMenus) {
            //根据一级菜单id 查询 该菜单得二级菜单。如果出现不确定有几级菜单 那么我们可以使用方法得递归调用
            first.setChildren(findChildren(permissionList, first.getPermissionid()));

        }
        return new CommonResult(2000,"查询成功",firstMenus);
    }

    // 修改权限按钮的点击事件回显数据
    @Override
    public CommonResult selectPermissionByRoleId(Integer id) {
        // 查询所有的权限
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("is_deleted",0);
        List<Permission> permissions = permissionMapper.selectList(wrapper);

        //设置层级关系
        List<Permission> firstMenus = new ArrayList<>();
        for (Permission first : permissions) {
            if (first!=null && first.getPid()!=null && first.getPid().equals(1)) {
                firstMenus.add(first);
            }
        }
        //为一级菜单设置二级菜单
        for (Permission first : firstMenus) {
            //根据一级菜单id 查询 该菜单得二级菜单。如果出现不确定有几级菜单 那么我们可以使用方法得递归调用
            first.setChildren(findChildren(permissions, first.getPermissionid()));

        }

        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("roleid",id);
        List<RolePermission> list = rolePermissionService.list(queryWrapper);

        List<String> collect = list.stream().map(item -> item.getPermissionid()).distinct().collect(Collectors.toList());
        Map<String,Object> map = new HashMap<>();
        map.put("data",firstMenus);
        map.put("collect",collect);
        return new CommonResult(2000,"查询成功",map);
    }

    //方法递归
    private List<Permission> findChildren(List<Permission> permissionList, Integer id) {
        List<Permission> children = new ArrayList<>();
        for (Permission p : permissionList) {
            if (p.getPid()!=null && p.getPid().equals(id)) {
                children.add(p);
            }
        }


        for (Permission child : children) {
            child.setChildren(findChildren(permissionList, child.getPermissionid()));
        }

        return children;

    }
}
